package controller;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * @author Gavin Greene
 * 
 * This GUI consists of the frame, Text fields, Menu, and Action Listeners, 
 * The events and data are sent to observer classes.
 * Various nastygrams in the form of error messages are generated 
 * when something goes against the contract of an item.
 * @author
 */
public class gui extends Observable {

	/**
	 * The main frame
	 */
	private JFrame my_frame;

	/**
	 * The menu bar declared as in innerclass at the bottom.
	 */
	private MenuBar my_menu;

	/**
	 * The text field where the data will be written to, this has mutators.
	 */
	private JTextArea my_text_field;

	private boolean my_file_selected_flag;
	

	/**
	 * The constructor for the GUI class creates the GUI itself, the frame, a menu, 
	 *  and the appropriate action listeners. 
	 */
	public gui(){
		super();

		my_frame = new JFrame();

		my_menu = new MenuBar();

		my_frame.add(my_menu, BorderLayout.NORTH);

		my_text_field = new JTextArea();

		
		setupTextField(my_text_field);
		
		JScrollPane scroll = new JScrollPane(my_text_field);
//		scroll.setPreferredScrollableViewportSize();
		JScrollBar bar = new JScrollBar();
		
		scroll.setVerticalScrollBar(bar);
		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

		my_frame.getContentPane().add(scroll);

		setupFrame(my_frame);
	}

	/**
	 * Sets the default behavior for the frame.
	 * @param the_frame
	 */
	private void setupFrame(JFrame the_frame){

		the_frame.setTitle("Schedule Generator- TCSS 360");
		the_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		the_frame.pack();
		the_frame.setVisible(true);
	}

	private void setupTextField(JTextArea the_text_field){
		the_text_field.setEditable(false);
		the_text_field.setWrapStyleWord(true);
		//the_text_field.setPreferredSize(new Dimension(450, 200));
		the_text_field.setText("Welcome to the Schedule Checker" + "\n" +
		"To checke a schedule, chose an approprate Schedule file from: File->Check Schedule");
	}

	/**
	 * Appends the text sent in to the text field.
	 * @param the_text the text to write.
	 */
	public void WriteTextField(String the_text){
		my_text_field.append(the_text + "\n");
		//my_text_field.setPreferredSize(preferredSize)

	}
	/**
	 * This is for when we don't want a space
	 * @param the_text
	 */
	public void AppendTextField(String the_text){
		my_text_field.append(the_text);
	}
	/**
	 * Clears the text field.
	 */
	public void ClearTextField(){
		my_text_field.setText("");
	}

	/**
	 * Selects a file. Observers notified
	 * @param the_filename
	 */
	private void SelectFile(){

		JFileChooser chooser = new JFileChooser(); 
		int returnVal = chooser.showOpenDialog(null);
		if(returnVal == JFileChooser.APPROVE_OPTION) {
			String Filename = chooser.getSelectedFile().getAbsolutePath();
			my_file_selected_flag = true;
			this.setChanged();
			notifyObservers(Filename);
		}
		//the user hit cancel
	}




	/* Getters and Setters */
	/**
	 * For the controller class; tells them if we have a file.
	 */
	public boolean hasFile(){
		return my_file_selected_flag;
	}
	/**
	 * we need to reset the file flag when the file isn't loaded correctly by the controller class.
	 * @param yes_no
	 */
	public void setFile(boolean yes_no){
		my_file_selected_flag = yes_no;
	}


	//Inner class for Menu
	/**
	 * Created for TCSS 343 - homework 6
	 * The menu bar includes menu buttons and action Listners that do various (non-suprising) things.
	 * This was done as an innerclass because it is so tightly coupled with the GUI class.
	 * 
	 * There is also an about bar, that gives some information about the programmers.
	 */
	public class MenuBar extends JMenuBar{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;


		MenuBar()
		{
			super();
			JMenu fileMenu = new JMenu("File");
			JMenu helpMenu = new JMenu("Help");

			SetupFileMenu(fileMenu);
			this.add(fileMenu);

			SetupHelpMenu(helpMenu);
			this.add(helpMenu);
		}

		private void SetupFileMenu(JMenu file_menu){

			JMenuItem menuOpen = new JMenuItem("Open Schedule");
			menuOpen.setMnemonic('o');
			menuOpen.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent arg0) {

					SelectFile();
				}
			});

			//the user hit cancel

			JMenuItem menuQuit = new JMenuItem("Quit");
			menuQuit.setMnemonic('q');
			menuQuit.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent arg0) {
					System.exit(0);
				}
			});
			file_menu.add(menuOpen);
			file_menu.addSeparator();
			file_menu.add(menuQuit);

		}
		

		private void SetupHelpMenu(JMenu help_menu){

			JMenuItem menuAbout = new JMenuItem("About");
			menuAbout.setMnemonic('a');
			menuAbout.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent arg0) {
					JOptionPane.showMessageDialog(null,
							"Made for TCSS 360 by:" + "\n" +
							"Gavin Greene" + "\n" +
							"Thach Nguyen" + "\n" +
					"Harmendeep Singh");

				}
			});

			help_menu.add(menuAbout);
		}



	}
}
